//
//  StageRecordTool.m
//  NewGame
//
//  Created by wwj on 15/10/9.
//  Copyright © 2015年 wwj. All rights reserved.
//

#define kFileName @"records.data"
#define kFilePath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:kFileName]

#import "StageRecordTool.h"
#import "StageRecordModel.h"

@interface StageRecordTool ()
@property (strong, nonatomic) NSMutableDictionary *allRecords;
@end

@implementation StageRecordTool

+(instancetype)sharedStageRecordTool{
    static StageRecordTool *sharedClass;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedClass = [[StageRecordTool alloc] init];
    });
    return sharedClass;
}

- (instancetype)init{
    self = [super init];
    if (self) {
       _allRecords = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath];
        if (_allRecords == nil) {
            _allRecords = [NSMutableDictionary dictionary];
            StageRecordModel *recordModel = [[StageRecordModel alloc] init];
            recordModel.no = 1;
            [self saveStageRecord:recordModel];
        }
    }
    return self;
}


/**
 *  存档
 *
 *  @param record
 */
-(void)saveStageRecord:(StageRecordModel *)record{
    
    if (record.no <=0) return;
    
    [_allRecords setObject:record forKey:@(record.no)];
    
    [NSKeyedArchiver archiveRootObject:_allRecords toFile:kFilePath];
}


/**
 *  读档
 *
 *  @param no
 *
 *  @return
 */
-(StageRecordModel *)stageRecordWithNo:(int)no{
    
    return _allRecords[@(no)] ;
}
@end
